package com.lai.note.adapter;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lai.note.R;

import java.util.List;

public class CustomRecyclerAdapter extends RecyclerView.Adapter<CustomRecyclerAdapter.ViewHolder>{
    public static class ViewHolder extends RecyclerView.ViewHolder{
        private final ImageView appIconImageView;
        private final TextView appNameTextView;
        public ViewHolder(View view){
            super(view);
            appIconImageView = view.findViewById(R.id.appIconImageView);
            appNameTextView = view.findViewById(R.id.appNameTextView);
        }

        public ImageView getAppIconImageView(){
            return appIconImageView;
        }

        public TextView getAppNameTextView(){
            return appNameTextView;
        }
    }

    private List<PackageInfo> packages;//数据来源

    public CustomRecyclerAdapter(List<PackageInfo> packages){
        super();
        this.packages = packages;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listapp_appitem,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PackageInfo packageInfo = packages.get(position);
        Context context = holder.getAppIconImageView().getContext();
        holder.getAppIconImageView().setImageDrawable(packageInfo.applicationInfo.loadIcon(context.getPackageManager()));
        holder.getAppNameTextView().setText(packageInfo.applicationInfo.loadLabel(context.getPackageManager()));
    }

    @Override
    public int getItemCount() {
        return packages.size();
    }
}
